package com.panshi.service.impl;

import com.panshi.dao.IFunctionDao;
import com.panshi.service.IFunctionService;
import com.panshi.tools.TransitionUtils;
import com.panshi.vo.FunctionVO;
import com.panshi.vo.KeyValueVO;
import com.panshi.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class FunctionServiceImpl implements IFunctionService {

    @Resource
    private IFunctionDao iFunctionDao;


    @Override
    public ResultVO<List<FunctionVO>> findFuncList() {
        List<FunctionVO> funcList = iFunctionDao.findFuncList();
        ResultVO<List<FunctionVO>> resultVO = new ResultVO<>();
        resultVO.setData(funcList);
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<FunctionVO> getFunInfo(long id) {
        ResultVO<FunctionVO> resultVO = new ResultVO<>();
        resultVO.setData(iFunctionDao.getFunInfo(id));
        return resultVO;
    }

    @Override
    public ResultVO<String> updateFunStatus(long id, int status) {
        iFunctionDao.updateFunStatus(id,status);
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<String> addFunInfo(FunctionVO functionVO) {
        // 判断新增的是否是目录 是目录的话吧pid改变
        if(functionVO.getType() == 1){
            functionVO.setPid(-1);
        }
        ResultVO<String> resultVO = new ResultVO<>();
        iFunctionDao.addFunInfo(functionVO);

        resultVO.setCode("200");
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<String> delFunInfo(long id) {
        log.info("删除菜单功能表的ID{}",id);
        // 删除该功能权限
        int status = iFunctionDao.delFunInfo(id);
        // 把所有角色的该权限删除
        int roleMenuStatus = iFunctionDao.delRoleFunInfo(id);
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<String> editFunInfo(FunctionVO functionVO) {
        log.info("修改菜单功能的值{}",functionVO);
        int status = iFunctionDao.editFunInfo(functionVO);
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<List<KeyValueVO>> initFunList() {
        List<FunctionVO> list = iFunctionDao.findFuncList();
        ResultVO<List<KeyValueVO>> resultVO = new ResultVO<>();
        resultVO.setData(TransitionUtils.getFunKeyValueVO(list));
        return resultVO;
    }

    @Override
    public ResultVO<List<KeyValueVO>> getRoleSelect(long roleId) {
        ResultVO<List<KeyValueVO>> resultVO = new ResultVO<>();
        List<FunctionVO> roleVOList = iFunctionDao.getRoleSelect(roleId);
        List<KeyValueVO> list = TransitionUtils.getFunKeyValueVO(roleVOList);
        resultVO.setData(list);
        return resultVO;
    }
}
